<template>
  <div class="login-view">
    <div class="login-container">
      <div class="login-card">
        <h2>登录</h2>
        
        <!-- 用户类型选择 -->
        <div class="user-type-selector">
          <el-radio-group v-model="loginForm.userType" size="large">
            <el-radio-button label="user">普通用户</el-radio-button>
            <el-radio-button label="merchant">商家</el-radio-button>
            <el-radio-button label="admin">管理员</el-radio-button>
          </el-radio-group>
        </div>
        
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :placeholder="userTypePlaceholder"
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="large" style="width: 100%;" @click="handleLogin" :loading="loading">
              登录
            </el-button>
          </el-form-item>
        </el-form>
        <div class="login-links">
          <router-link to="/register" v-if="loginForm.userType !== 'admin'">
            还没有账号？立即注册
          </router-link>
          <span v-else class="admin-note">管理员账号由系统管理员创建</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'LoginView',
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: '',
        remember: false,
        userType: 'user'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    userTypePlaceholder() {
      switch (this.loginForm.userType) {
        case 'user':
          return '请输入用户名'
        case 'merchant':
          return '请输入商家用户名'
        case 'admin':
          return '请输入管理员用户名'
        default:
          return '请输入用户名'
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginFormRef.validate()
        this.loading = true
        
        // 调用登录API
        const response = await fetch('http://localhost:2077/api/auth/login', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            username: this.loginForm.username,
            password: this.loginForm.password,
            userType: this.loginForm.userType
          })
        })
        
        const result = await response.json()
        
        if (result.success) {
          // 登录成功，保存token和用户信息
          localStorage.setItem('token', result.data.token)
          localStorage.setItem('userRole', result.data.userType)
          localStorage.setItem('username', this.loginForm.username)
          
          // 根据用户类型保存不同的用户信息
          if (result.data.user) {
            localStorage.setItem('userId', result.data.user.userId)
          } else if (result.data.merchant) {
            localStorage.setItem('merchantId', result.data.merchant.merchantId)
          } else if (result.data.admin) {
            localStorage.setItem('adminId', result.data.admin.adminId)
          }
          
          ElMessage.success('登录成功！')
          
          // 触发全局状态更新
          this.$nextTick(() => {
            // 通过事件总线或直接调用父组件方法来更新状态
            window.dispatchEvent(new Event('storage'))
          })
          
          // 根据用户类型跳转到不同页面
          this.redirectAfterLogin()
        } else {
          ElMessage.error(result.message || '登录失败')
        }
        
      } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error('网络错误，请稍后重试')
      } finally {
        this.loading = false
      }
    },
    
    redirectAfterLogin() {
      const { userType } = this.loginForm
      
      switch (userType) {
        case 'user':
          this.$router.push('/')  // 普通用户登录后返回首页
          break
        case 'merchant':
          this.$router.push('/merchant/dashboard')
          break
        case 'admin':
          this.$router.push('/admin/dashboard')
          break
        default:
          this.$router.push('/')
      }
    }
  }
}
</script>

<style scoped>
.login-view {
  min-height: 100vh;
  background: var(--gradient-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-md);
  position: relative;
  overflow: hidden;
}

.login-view::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><radialGradient id="a" cx="50%" cy="50%"><stop offset="0%" stop-color="rgba(255,255,255,0.1)"/><stop offset="100%" stop-color="rgba(255,255,255,0)"/></radialGradient></defs><circle cx="20" cy="20" r="2" fill="url(%23a)"/><circle cx="80" cy="20" r="2" fill="url(%23a)"/><circle cx="20" cy="80" r="2" fill="url(%23a)"/><circle cx="80" cy="80" r="2" fill="url(%23a)"/><circle cx="50" cy="50" r="3" fill="url(%23a)"/></svg>') repeat;
  opacity: 0.3;
  animation: float 20s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.login-container {
  width: 100%;
  max-width: 450px;
  position: relative;
  z-index: 1;
}

.login-card {
  background: var(--background-primary);
  padding: var(--spacing-xl);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--gradient-primary);
}

h2 {
  text-align: center;
  margin-bottom: var(--spacing-lg);
  color: var(--text-primary);
  font-weight: 700;
  font-size: 2rem;
  position: relative;
}

h2::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: var(--gradient-secondary);
  border-radius: var(--radius-xl);
}

.user-type-selector {
  margin-bottom: var(--spacing-xl);
  text-align: center;
}

.el-radio-group {
  width: 100%;
}

.el-radio-button {
  flex: 1;
}

.el-radio-button__inner {
  width: 100%;
  border-radius: var(--radius-md) !important;
  border: 2px solid var(--border-light) !important;
  background: var(--background-accent) !important;
  color: var(--text-secondary) !important;
  font-weight: 500 !important;
  transition: all 0.3s ease !important;
}

.el-radio-button__inner:hover {
  border-color: var(--primary-light) !important;
  background: var(--background-primary) !important;
  color: var(--text-primary) !important;
}

.el-radio-button.is-active .el-radio-button__inner {
  background: var(--gradient-primary) !important;
  border-color: var(--primary-color) !important;
  color: white !important;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3) !important;
}

.el-form-item {
  margin-bottom: var(--spacing-lg);
}

.el-input {
  height: 50px;
}

.el-input__wrapper {
  background: var(--background-accent) !important;
  border: 2px solid transparent !important;
  border-radius: var(--radius-lg) !important;
  transition: all 0.3s ease !important;
  box-shadow: none !important;
}

.el-input__wrapper:hover {
  border-color: var(--primary-light) !important;
  background: var(--background-primary) !important;
}

.el-input__wrapper.is-focus {
  border-color: var(--primary-color) !important;
  background: var(--background-primary) !important;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
}

.el-button {
  height: 50px !important;
  font-size: 1.1rem !important;
  font-weight: 600 !important;
  border-radius: var(--radius-lg) !important;
  transition: all 0.3s ease !important;
}

.el-button--primary {
  background: var(--gradient-primary) !important;
  border: none !important;
  position: relative;
  overflow: hidden;
}

.el-button--primary::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s;
}

.el-button--primary:hover::before {
  left: 100%;
}

.el-checkbox {
  margin: var(--spacing-md) 0;
}

.login-links {
  text-align: center;
  margin-top: var(--spacing-lg);
  padding-top: var(--spacing-md);
  border-top: 1px solid var(--border-light);
}

.login-links a {
  color: var(--primary-color);
  text-decoration: none;
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.login-links a::after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: -2px;
  left: 50%;
  background: var(--gradient-secondary);
  transition: all 0.3s ease;
}

.login-links a:hover {
  color: var(--secondary-color);
}

.login-links a:hover::after {
  width: 100%;
  left: 0;
}

.admin-note {
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-style: italic;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-view {
    padding: var(--spacing-sm);
  }
  
  .login-card {
    padding: var(--spacing-lg);
  }
  
  h2 {
    font-size: 1.75rem;
  }
  
  .el-radio-button__inner {
    font-size: 0.9rem !important;
    padding: 8px 12px !important;
  }
}

/* 输入框图标样式 */
.el-input__prefix-inner {
  color: var(--text-secondary) !important;
}

.el-input__wrapper.is-focus .el-input__prefix-inner {
  color: var(--primary-color) !important;
}
</style> 